import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

//创建store 对象
const store = new Vuex.Store({
	//state中是需要管理的全局变量
	state:{
		num:0,
		userid:0,
		nickname:'',
		avaurl:'',
		hasLogin:false
	},
	actions:{
		init(){
			this.commit('initNum',100)
		}
	},
	//mutations 是操作state中变量的方法
	mutations:{
		initNum(state,num){
			state.num = num
		},
		login(state,payload){  //登录成功修改 全局变量
			state.userid = payload.id;
			state.nickname = payload.nickname
			state.avaurl = payload.avaurl
			state.hasLogin = true;
		},
		loginOut(state){     //退出登录修改 全局变量
			state.userid = 0;
			state.nickname = '';
			state.avaurl = '';
			state.hasLogin = false;
		}
	}

})
export default store
